<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/22
 * Time: 16:02
 */
namespace Home\Model;
use Common\Library\BusinessException;
use Think\Log;
use Think\Model;
use Common\Library\Constant;

class AjaxModel extends Model
{
    private function post($url, $xml_data)
    {
        //$header = array('Content-type: application/json');//定义content-type为json
        $ch=curl_init($url); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url); //设置链接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //设置是否返回信息
        //curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data); //POST数据
        Log::write("请求数据：".$xml_data, Log::DEBUG);
        $response = curl_exec($ch); //接收返回信息
        if(curl_errno($ch)) //出错则显示错误信息
        {
            print curl_error($ch);
        }
        curl_close($ch); //关闭curl链接

        Log::write("响应数据：".$response, Log::DEBUG);
        return $response;
    }

    private function genSign(array $param) {
        //0. 删除原数据中自带的sign值,防止干扰计算结果
        unset($param['sign']);

        //1. 按key由a到z排序
        ksort($param);
        foreach ($param as $key => $value) {
            if (is_array($value)) {
                $param[$key] = genSign($value);
            }
        }

        //2. 生成以&符链接的key=value形式的字符串
        $paramString = urldecode(http_build_query($param));

        //3. 拼接我们的服务秘钥，并md5加密
        $sign = md5($paramString . 'secret@huoyan.com');

        return $sign;
    }

    public function ajaxPost($model, $xml_data)
    {
        $api_url = C('API_SERVER_HOST');
        $sign = $this->genSign($xml_data);
        $url = $api_url."/Home/".$model;
        $xml_data['sign'] = $sign;

        //Log::write("POST-AJAX请求发送数据：".$xml_data, Log::DEBUG);

        $res = $this->post($url, json_encode($xml_data));

        Log::write("接收响应数据：".$res, Log::DEBUG);
        
        return $res;
    }
    public function ajaxPosts($url, $xml_data)
    {
        $sign = $this->genSign($xml_data);
        $xml_data['sign'] = $sign;

        //Log::write("POST-AJAX请求发送数据：".$xml_data, Log::DEBUG);

        $res = $this->post($url, json_encode($xml_data));

        Log::pay_write("接收响应数据：".$res.'   url:'.$url.'    data'.json_encode($xml_data), Log::DEBUG);

        return $res;
    }
    public  function   ajaxPostByUrl($xml_data,$url){
        $sign = $this->genSign($xml_data);
        $xml_data['sign'] = $sign;
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($xml_data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $response = curl_exec($ch);
        return $response;
    }
    public  function   ajaxPostUrl($xml_data,$url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($xml_data));
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $response = curl_exec($ch);
        return $response;
    }
    
}
